package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.bumptech.glide.Glide;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.User;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.hengxinguotong.zhihuichengjian.widget.view.CircleImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liux on 2016/7/11.
 */
public class GroupGridAdapter extends BaseAdapter {
    private List<User> userInfos;
    private final Context context;
    private boolean isEdit = false;
    private boolean canEdit = false;

    public List<User> getUserInfos() {
        return userInfos;
    }

    public void setUserInfos(List<User> userInfos) {
        this.userInfos = userInfos;
    }

    public boolean isEdit() {
        return isEdit;
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    public GroupGridAdapter(Context context, List<User> userInfos, boolean canEdit) {
        this.context = context;
        this.userInfos = userInfos;
        this.canEdit = canEdit;
    }

    @Override
    public int getCount() {
//        if(canEdit){
//            return userInfos.size() + 1;
//        }else{
        return userInfos.size();
//        }
    }

    @Override
    public Object getItem(int position) {
        return userInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_group_gridview, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        User user = userInfos.get(position);
        Glide.with(context).load(user.getHeadPortrait()).placeholder(R.mipmap.icon_avater_defalut).dontAnimate().error(R.mipmap.icon_avater_defalut).into(holder.userIv);

//        Glide.with(context).load(Utils.SERVER_PIC + user.getHeadPortrait()).placeholder(R.mipmap.icon_avater_defalut).error(R.mipmap.icon_avater_defalut).dontAnimate().into(holder.userIv);
        holder.nameTv.setText(user.getName());
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.user_iv)
        CircleImageView userIv;
        @BindView(R.id.name_tv)
        HXTextView nameTv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

